﻿/*************************************************************************************

- CLR版本：        4.0.30319.42000
- 类 名 称：       PlcInovanceH5u
- 机器名称：       YTE
- 命名空间：       LD.Common.PLC
- 文 件 名：       PlcInovanceH5u
- 创建时间：       2022/1/15 13:47:06
- 作    者：       YTE
- 说   明：
- 修改时间：
- 修 改 人：
  *************************************************************************************/

using LD.Host;
using LdCommunication;
using LdCommunication.Core;
using LdCommunication.Profinet.Inovance;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace LD.PLC
{
    /// <summary>
    /// 汇川H5U
    /// </summary>
    public class PlcInovanceH5u : PLCAbstract
    {
        private TaskEx taskEx = new TaskEx();
        private InovanceTcpNet h5uNet;
        private Status status = Status.失败;
        public override event ConnectionStatusDelegate StatusEvent;
        public PlcInovanceH5u()
        {
            h5uNet = new InovanceTcpNet
            {
                Series = InovanceSeries.H5U

            };
            h5uNet.DataFormat = DataFormat.CDAB;
        }
        public override Status Status
        {
            get { return status; }
            set
            {
                status = value;
                StatusEvent?.BeginInvoke(Name, value, null, h5uNet);
            }
        }

        public override OperateResult<byte[]> Read(string address, ushort val)
        {
            return h5uNet.Read(address, val);
        }

        public override OperateResult<bool[]> ReadBool(string address, ushort val)
        {
            return h5uNet.ReadBool(address, val);
        }

        public override OperateResult<bool> ReadBool(string address)
        {
            return h5uNet.ReadBool(address);
        }

        public override OperateResult<int> ReadInt(string address)
        {
            return h5uNet.ReadInt32(address);
        }

        public override OperateResult<int[]> ReadInt(string address, ushort val)
        {
            return h5uNet.ReadInt32(address, val);
        }

        public override OperateResult<short> ReadInt16(string address)
        {
            return h5uNet.ReadInt16(address);
        }

        public override OperateResult<short[]> ReadInt16(string address, ushort val)
        {
            return h5uNet.ReadInt16(address, val);
        }

        public override OperateResult<string> ReadString(string address, ushort val)
        {
            return h5uNet.ReadString(address, val);
        }

        public override void Start()
        {
            if (HDParam != null)
            {
                h5uNet.IpAddress = HDParam.Address;
                h5uNet.Port = HDParam.Port;
                h5uNet.ConnectTimeOut = HDParam.TimeOut;
                h5uNet.ReceiveTimeOut = HDParam.TimeOut;
                taskEx.Start(Connection, null);
            }
        }

        public override void Stop()
        {
            taskEx?.Cancel();
            h5uNet?.ConnectClose();
        }
        private void Connection(object param)
        {
            try
            {
                Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}PLC状态运行中。。。");

                if (status == Status.失败)
                {
                    OperateResult operate = h5uNet.ConnectServer();
                    if (operate.IsSuccess)
                    {
                        Status = Status.成功;
                        if (!string.IsNullOrEmpty(HDParam.Heartbeat))
                        {
                            ushort val = 1;
                            Write(HDParam.Heartbeat, val);
                        }
                    }
                    else
                    {
                        Status = Status.失败;
                    }
                }

            }
            catch (Exception ex)
            {
                Status = Status.失败;
                Log4.Error(ex);
            }

            Task.Delay(1100).Wait();
        }
        public override OperateResult Write(string address, short val)
        {
            return h5uNet.Write(address, val);
        }

        public override OperateResult Write(string address, int val)
        {
            return h5uNet.Write(address, val);
        }

        public override OperateResult Write(string address, bool val)
        {
            return h5uNet.Write(address, val);
        }

        public override OperateResult Write(string address, string val)
        {
            return h5uNet.Write(address, val);
        }

        public override OperateResult Write(string address, byte[] val)
        {
            return h5uNet.Write(address, val);
        }

        public override OperateResult<float> ReadFloat(string address)
        {
            return h5uNet.ReadFloat(address);
        }

        public override OperateResult<float[]> ReadFloat(string address, ushort val)
        {
            return h5uNet.ReadFloat(address, val);
        }

        public override OperateResult Write(string address, float val)
        {
            return h5uNet.Write(address, val);
        }

        public override bool Wait(string address, short val, int timeout)
        {
            bool result = false;
            var time = h5uNet.Wait(address, val, 200, timeout);
            if (time.IsSuccess)
            {
                result = true;
            }
            return result;
        }

        public override bool Wait(string address, bool val, int timeout)
        {
            bool result = false;
            var time = h5uNet.Wait(address, val, 200, timeout);
            if (time.IsSuccess)
            {
                result = true;
            }
            return result;
        }

        public override OperateResult Write(string address, double val)
        {
            return h5uNet.Write(address, val);
        }
    }
}
